package com.apes.enquiry.supplier.santouliubi;

import com.alibaba.fastjson.JSONObject;
import com.apes.enquiry.supplier.SupplierInterfaceApi;
import com.apes.framework.util.MD5Util;
import com.apes.framework.util.MapUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class SantouliubiApi extends SupplierInterfaceApi {

    @Value("${santouliubi.url}")
    private String HOST;
    public final static String MERCHANTS_ID = "20000083";
    @Value("${santouliubi.secretKey}")
    private String SECRET_KEY;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取 token
     */
    private String getToken(JSONObject jsonObject) {
        String token = MD5Util.encrypt(jsonObject.toString() + "&secretKey=" + SECRET_KEY);
        return token;
    }

    /**
     * 请求头
     *
     * @param jsonObject
     * @return
     */
    private JSONObject getHeader(JSONObject jsonObject) {
        return (JSONObject) JSONObject.toJSON(MapUtil.mapper("token", getToken(jsonObject)));
    }

    /**
     * 门店申请注册
     *
     * @param body
     * @return
     */
    public JSONObject applyFor(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "create");
        json.put("params", body);
        json.put("id", body.get("merchantsRelatedNumber"));
        String method = "/v1/merchants/garage/apply" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 查询已经审核通过的门店列表
     *
     * @param body
     * @return
     */
    public JSONObject getShopInformation(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "get-list");
        json.put("params", body);
        json.put("id", "0000001");
        String method = "/v1/merchants/garage/garage" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 调用三头六臂接口获取门店详情
     * @param body
     * @return
     */
    public JSONObject getShopManagement(JSONObject body){
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "get-detail");
        json.put("params", body);
        json.put("id", body.getString("merchantsRelatedNumber"));
        String method = "/v1/merchants/garage/apply" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 启用
     * @param body
     * @return
     */
    public JSONObject enable(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "enable");
        json.put("params", body);
        json.put("id", "0000003");
        String method = "/v1/merchants/garage/garage" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 禁用
     * @param body
     * @return
     */
    public JSONObject disable(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "disable");
        json.put("params", body);
        json.put("id", "0000004");
        String method = "/v1/merchants/garage/garage" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 询价
     * @param body
     * @return
     */
    public JSONObject queryPrice(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "get-products");
        json.put("params", body);
        json.put("id", "0000002");
        String method = "/v1/garage/car/car-product" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 获取商品列表
     * @param body
     * @return
     */
    public JSONObject queryProduct(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "get-list");
        json.put("params", body);
        json.put("id", "0000008");
        String method = "/v1/garage/search/product/product" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 下订单
     * @param body
     * @return
     */
    public JSONObject orderForm(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "create");
        json.put("params", body);
        json.put("id", "0000005");
        String method = "/v1/garage/order/order" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 查订单
     * @param body
     * @return
     */
    public JSONObject getOrder(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "get-detail");
        json.put("params", body);
        json.put("id", "0000006");
        String method = "/v1/garage/order/order" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }

    /**
     * 确认收货
     * @param body
     * @return
     */
    public JSONObject receipt(JSONObject body) {
        JSONObject json = new JSONObject();
        json.put("jsonrpc", "2.0");
        json.put("method", "confirm-receipt");
        json.put("params", body);
        json.put("id", "0000007");
        String method = "/v1/garage/order/order" + "?token="+ getToken(json);
        return executePost(HOST + method, json, getHeader(json));
    }
}
